<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/18
 * Time: 15:40
 */

namespace addons\seller\controller;



use app\common\Code;
use app\common\model\Trade;
use app\common\model\TradeOrder;
use app\common\OrderStatus;
use app\common\Session;

class Order extends Common
{
    /**
     * 获取商家列表
     * @return mixed
     */
    public function list_()
    {
        $this->check_login();
        $seller = Session::get_local_seller();
        if (empty($seller)) {
            response_json(Code::NO_LOGIN);
        }
        #获取订单状态
        $order_status = OrderStatus::get_order_status();
        $this->assign("order_status", $order_status);
        $this->assign("wait_for_order", OrderStatus::DAIFAHUO);  # 待接单
        $this->assign("accept_order", OrderStatus::YIJIEDAI);  #已接单
        #订单列表数据
        $param = input();
        $this->assign("param", $param);
        #筛选商家订单
        $cond['seller_id'] = $seller->id;
        $list_ = \app\common\model\Order::list_($param);
        $this->assign("list_", $list_);
        if (input('export')) {
            goto EXPORT;
        }
        return $this->fetch();
        EXPORT:
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=data.xls");
        return $this->fetch("export_seller_order");
    }
    /**
     * 订单详情
     * @return mixed
     */
    public function order_details()
    {
        $this->check_login();
        $param = input();
        $order_data = \app\common\model\Order::get_one_order_($param['order_id']);  #获取订单信息
        $details_ = \app\common\model\Order::order_details($param['order_id']);     #获取子订单信息
        $relation_ = TradeOrder::get_trade_by_order_id($param['order_id']);
        $trade = Trade::get_trade_by_code(isset($relation_->code)?$relation_->code:0);
        $this->assign("order_data", $order_data);
        $this->assign("order_detail", $details_);
        $this->assign("trade", $trade);
        return $this->fetch();
    }

    /**
     *确认接单
     * 参数 order_id
     */
    public function accept_order()
    {
        $param = input();
        if (!isset($param['order_id'])) {
            response_json(Code::PARAM_ERROR);
        }
        $order_id = $param['order_id'];
        /** @var  Order $order */
        $order = \app\common\model\Order::get(["order_id" => $order_id]);
        if (empty($order)) {
            response_json(Code::ORDER_EMPTY);
        }
        if ($order->order_status_id >= OrderStatus::YIJIEDAI) {
            response_json(Code::ORDER_YIJIEDAN);
        }
        OrderStatus::yijiedan($order);
        response_json(Code::NONE);
    }

    /**
     *确认送达
     */
    public function querenshouhuo()
    {
        $param = input();
        if (!isset($param['order_id'])) {
            response_json(Code::PARAM_ERROR);
        }
        $order_id = $param['order_id'];
        /** @var  Order $order */
        $order = \app\common\model\Order::get(["order_id" => $order_id]);
        if (empty($order)) {
            response_json(Code::ORDER_EMPTY);
        }
        if ($order->order_status_id >= OrderStatus::QUERENSHOUHUO) {
            response_json(Code::FAIL);
        }
        OrderStatus::rebates($order,
            sprintf("%s订单完成，系统执行返利", $order->order_id)
        );
        OrderStatus::querenshouhuo($order);
        response_json(Code::NONE);
    }
    /**
     *商家社区订单商品统计
     */
    public function order_goods_()
    {
        $this->check_login();
        $seller = Session::get_local_seller();
        if (empty($seller)) {
            response_json(Code::NO_LOGIN);
        }
        $order_status = OrderStatus::DAIFAHUO;
        $data = \addons\admin\service\Order::community_order_goods($seller->id,$order_status);
        $res = [];
        foreach($data as $c_k=>$c_v)
        {
            SUPPLY_SELLER_:
            $k = $c_v['community_id'];
            if(isset($res[$k]))
            {
                $goods_id = $c_v["goods_id"];
                $res[$k]['community_id'] = $c_v['community_id'];
                $res[$k]['community_name'] = $c_v['community_name'];
                SUPPLY_GOODS:
                if (isset($res[$k]["data"][$goods_id])) {
                    $res[$k]["data"][$goods_id]["goods_id"] = $goods_id;
                    $res[$k]["data"][$goods_id]["goods_name"] = $c_v["goods_name"];
                    $res[$k]["data"][$goods_id]["price"] = $c_v["price"];
                    $res[$k]["data"][$goods_id]["count"] += $c_v["count"];
                } else {
                    $res[$k]["data"][$goods_id]["goods_id"] = 0;
                    $res[$k]["data"][$goods_id]["goods_name"] = 0;
                    $res[$k]["data"][$goods_id]["price"] = 0;
                    $res[$k]["data"][$goods_id]["count"] = 0;
                    goto SUPPLY_GOODS;
                }
            }else
            {
                $res[$k] = [];
                $res[$k]['data'] = [];
                goto SUPPLY_SELLER_;
            }
        }
        $this->assign("order_goods", $res);
        return $this->fetch();
    }
}